What is wrong with the following code segment?
Point myPoint = new Point(); // construct a point at x=0, y=0 myPoint.move(); // move the point to a new location
The second statement calls the move()
method of the Point,
but does not supply information about where to move the point.
In this case, saying what method to run is not enough.
The method needs information about what it is to do.
When you look at the description of class Point
the move()
method
is described as follows:
public void move(int x, int y); // change (x,y) of a point object
This description says that the method requires two parameters:
int
. It will become the new value for x.
int
. It will become the new value for y.
"Dot notation" is used to specify which object, and what method of that object to run. The parameter list of the method supplies the method with data. Here are some examples:
Point pointA = new Point(); Point pointB = new Point( 94, 172 ); pointA.move( 45, 82 ); pointB.move( 24-12, 34*3 - 45 ); int col = 87; int row = 55; pointA.move( col, row ); pointB.move( col-4; row*2 + 34 );
You can put expressions into parameter lists as long as the expression evaluates to the type expected by the method. Of course, the expressions are evaluated (using the usual rules) before the method starts running.